package com.yunhe.inspect.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunhe.common.constant.InSpectConsts;
import com.yunhe.common.constant.OfficeConts;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.authority.MessageCenter;
import com.yunhe.common.model.authority.Organization;
import com.yunhe.common.model.authority.RealStation;
import com.yunhe.common.model.inspect.RealStationOfficeCompanyVo;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.common.model.office.Office;
import com.yunhe.common.util.CollectionUtil;
import com.yunhe.common.util.DateTimeUtils;
import com.yunhe.common.util.StringUtil;
import com.yunhe.inspect.client.AuthorityClient;
import com.yunhe.inspect.domain.DataInspectOrder;
import com.yunhe.inspect.domain.DataInspectOrderItem;
import com.yunhe.inspect.domain.DataInspectPlan;
import com.yunhe.inspect.domain.DataInspectPlanItem;
import com.yunhe.inspect.model.*;
import com.yunhe.inspect.repository.DataInspectPlanItemRepository;
import com.yunhe.inspect.repository.DataInspectPlanRepository;
import com.yunhe.inspect.service.DataInspectOrderService;
import com.yunhe.inspect.service.DataInspectPlanService;
import com.yunhe.inspect.util.MyHeaderUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * @description 巡检计划表
 * @author misty
 * @date 2021-11-09
 */
@Service
@Slf4j
public class DataInspectPlanServiceImpl implements DataInspectPlanService {

    @Autowired
    private DataInspectPlanRepository dataInspectPlanRepository;

    @Autowired
    private DataInspectPlanItemRepository dataInspectPlanItemRepository;

    @Autowired
    private MyHeaderUtil myHeaderUtil;

    @Autowired
    private AuthorityClient authorityClient;

    @Autowired
    private DataInspectOrderService dataInspectOrderService;


    private static LinkedBlockingQueue<DataInspectOrder> dataListBOQueue = new LinkedBlockingQueue<>();


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void createDataInspectPlan(DataInspectPlanBo dataInspectPlanBo) throws ArgumentErrorException, InterruptedException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            throw new ArgumentErrorException("请选择项目维护巡检计划!");
        }
        List<String> scopeOfUse = dataInspectPlanBo.getScopeOfUse();
        if(CollectionUtil.isEmpty(scopeOfUse)){
            throw new ArgumentErrorException("请选择巡检计划的使用范围！");
        }
        DataInspectPlan dataInspectPlan = new DataInspectPlan();
        BeanUtils.copyProperties(dataInspectPlanBo,dataInspectPlan);
        dataInspectPlan.setScopeOfUse(String.join(";",scopeOfUse));
        dataInspectPlan.setStationId(Long.valueOf(projectCode));
        dataInspectPlan.setCreator(headerUser);
        //判断计划开始时间是否当前时间
        if(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), new Date()).
                equals(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), dataInspectPlan.getInspectPlanStartTime()))){
            dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.HaveInHand.value());
            dataInspectPlan.setRecordTime(new Date());
        }
        dataInspectPlanRepository.createDataInspectPlanSelective(dataInspectPlan);
        List<Integer> itemPlanIds = dataInspectPlanBo.getItemPlanIds();
        if(CollectionUtil.isNotEmpty(itemPlanIds)){
            itemPlanIds.forEach(getIntegerConsumer(dataInspectPlan));
        }
        //判断计划开始时间是否当前时间
        if(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), new Date()).
                equals(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), dataInspectPlan.getInspectPlanStartTime()))){
            //异步生成 对应的巡检工单
            CompletableFuture noArgsFuture = CompletableFuture.runAsync(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    log.info("异步生成巡检工单线程开启");
                    runAsync(dataInspectPlan,projectCode,scopeOfUse);
                }
            });
        }
    }

    private synchronized  void runAsync(DataInspectPlan dataInspectPlan, String projectCode, List<String> scopeOfUse) throws InterruptedException {
        long startTime = System.currentTimeMillis();
        //创建线程池 批量处理数据
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.submit(() -> {
            batchDataProcessing();
        });
        ResponseEntity<ResultObject<List<RealStationOfficeCompanyVo>>> responseEntity = null;
        //如果全部  推送 当前项目下所有真实园区的已租或已售厂房数据
        if("00".equals(dataInspectPlan.getScopeOfUse())){
            responseEntity = authorityClient.searchStationAndOfficeAndCompanyByStationId(Long.valueOf(projectCode));
        }else {
            responseEntity = authorityClient.searchStationAndOfficeAndCompany(scopeOfUse);
        }
        if(responseEntity != null){
            if (responseEntity.getStatusCode().is2xxSuccessful()) {
                List<RealStationOfficeCompanyVo> results = responseEntity.getBody().getResults();
                InSpectConsts.InspectPlanCycle inspectPlanCycle = InSpectConsts.InspectPlanCycle.byValue(dataInspectPlan.getInspectPlanCycle());
                if(CollectionUtil.isNotEmpty(results)){
                    for (RealStationOfficeCompanyVo result : results) {
                        DataInspectOrder dataInspectOrder = new DataInspectOrder();
                        dataInspectOrder.setInspectOrderNo(result.getRealStationName());
                        dataInspectOrder.setCreator(dataInspectPlan.getCreator());
                        dataInspectOrder.setInspectType(dataInspectPlan.getInspectType());
                        dataInspectOrder.setInspectOrderGenerationTime(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), new Date()));
                        //App排序字段
                        dataInspectOrder.setAppOrder("4");
                        Date inspectOrderExpirationTime = null;
                        Date date = new Date();
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(date);
                        switch (inspectPlanCycle){
                            case Day:
                                calendar.add(Calendar.DATE, 1);
                                dataInspectOrder.setAppOrder("2");
                                break;
                            case Week:
                                calendar.add(Calendar.DATE, 7);
                                break;
                            case Month:
                                calendar.add(Calendar.MONTH, 1);
                                break;
                            case Season:
                                calendar.add(Calendar.MONTH, 3);
                                break;
                            case Year:
                                calendar.add(Calendar.YEAR, 1);
                                break;
                        }
                        //根据当前时间
                        dataInspectOrder.setInspectOrderExpirationTime(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), calendar.getTime()));
                        dataInspectOrder.setInspectOrderTitle(dataInspectPlan.getInspectPlanTitle());
                        dataInspectOrder.setOrgId(dataInspectPlan.getOrgId());
                        dataInspectOrder.setCompanyId(result.getCompanyId());
                        dataInspectOrder.setCompanyName(result.getCompanyTitle());
                        dataInspectOrder.setOfficeId(result.getOfficeId());
                        dataInspectOrder.setOfficeName(result.getOfficeTitle());
                        dataInspectOrder.setStationId(Long.valueOf(projectCode));
                        dataInspectOrder.setInspectPlanId(Long.valueOf(dataInspectPlan.getId()));
                        //根据到期时间计算确定巡检单期限，临近3天到期的复审单为 1即将到期，大于3天为 0正常
                        int inspectRemainingDays = 0;
                        try {
                            inspectRemainingDays = daysBetween(new Date(), new SimpleDateFormat("yyyy-MM-dd").parse(dataInspectOrder.getInspectOrderExpirationTime()));
                            dataInspectOrder.setInspectRemainingDays(inspectRemainingDays);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        if (inspectRemainingDays > 3){
                            dataInspectOrder.setInspectDeadlineStatus("0");
                        } else if (inspectRemainingDays <= 3 && inspectRemainingDays >=0){
                            dataInspectOrder.setInspectDeadlineStatus("1");
                        }
                        //获取对应的计划的检查项
                        List<InspectPlanItem> inspectPlanItems = dataInspectPlanItemRepository.searchDataInspectPlanItemByInspectPlanId(dataInspectPlan.getId());
                        if(CollectionUtil.isNotEmpty(inspectPlanItems)){
                            List<DataInspectOrderItem> dataInspectOrderItems = new ArrayList<>();
                            for (InspectPlanItem inspectPlanItem : inspectPlanItems) {
                                DataInspectOrderItem dataInspectOrderItem = new DataInspectOrderItem();
                                dataInspectOrderItem.setItemPlanId(inspectPlanItem.getId());
                                dataInspectOrderItems.add(dataInspectOrderItem);
                            }
                            dataInspectOrder.setDataInspectOrderItemList(dataInspectOrderItems);
                        }
                        dataListBOQueue.add(dataInspectOrder);
                    }
                }
            }
        }
        dataListBOQueue.add(new DataInspectOrder());
        long endTime = System.currentTimeMillis();
        log.info("数据处理所需时间：{}",(endTime-startTime));
        log.info("数据处理结束");
        executorService.shutdown();
        executorService.awaitTermination(20, TimeUnit.SECONDS);
    }

    /**
     * 计算两个日期之间相差的天数
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate,Date bdate)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        try {
            smdate=sdf.parse(sdf.format(smdate));
            bdate=sdf.parse(sdf.format(bdate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }

    private void batchDataProcessing() {
        try {
            for (; ; ) {
                List<DataInspectOrder> dataLists = new ArrayList<>(100);
                for (int i = 0; i < 50; i++) {
                    DataInspectOrder take = dataListBOQueue.take();
                    if (take == null || take.getInspectPlanId() ==null) {
                        if (!CollectionUtils.isEmpty(dataLists)) {
                            dataInspectOrderService.batchDataInspectOrder(dataLists);
                        }
                        return;
                    } else {
                        dataLists.add(take);
                    }
                }
                if (!CollectionUtils.isEmpty(dataLists)) {
                    dataInspectOrderService.batchDataInspectOrder(dataLists);
                    dataLists.clear();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private Consumer<Integer> getIntegerConsumer(DataInspectPlan dataInspectPlan) {
        return itemPlanId -> {
            DataInspectPlanItem dataInspectPlanItem = new DataInspectPlanItem();
            dataInspectPlanItem.setInspectPlanId(dataInspectPlan.getId());
            dataInspectPlanItem.setItemPlanId(itemPlanId);
            dataInspectPlanItemRepository.createDataInspectPlanItemSelective(dataInspectPlanItem);
        };
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDataInspectPlan(List<Integer> ids) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        if(CollectionUtil.isNotEmpty(ids)){
            dataInspectPlanRepository.deleteDataInspectPlanByIds(ids,headerUser);
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public  void updateDataInspectPlan(DataInspectPlanBo dataInspectPlanBo) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            throw new ArgumentErrorException("请选择项目维护巡检计划!");
        }
        List<String> scopeOfUse = dataInspectPlanBo.getScopeOfUse();
        if(CollectionUtil.isEmpty(scopeOfUse)){
            throw new ArgumentErrorException("请选择巡检计划的使用范围！");
        }
        DataInspectPlan dataInspectPlan = new DataInspectPlan();
        BeanUtils.copyProperties(dataInspectPlanBo,dataInspectPlan);
        dataInspectPlan.setStationId(Long.valueOf(projectCode));
        dataInspectPlan.setUpdator(headerUser);
        dataInspectPlan.setScopeOfUse(String.join(";",scopeOfUse));
        DataInspectPlan dataInspectPlan1 = dataInspectPlanRepository.searchDataInspectPlanByPrimaryKey(dataInspectPlan.getId());
        if(dataInspectPlan1 ==  null){
            throw new ArgumentErrorException("选择的对应的巡检计划不存在！");
        }
        dataInspectPlan.setCreator(dataInspectPlan1.getCreator());
        //dataInspectPlan.setRecordTime(dataInspectPlan1.getRecordTime());
        //判断计划开始时间是否当前时间
        if(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), new Date()).
                equals(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), dataInspectPlan.getInspectPlanStartTime()))){
            dataInspectPlan.setRecordTime(new Date());
            dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.HaveInHand.value());
        }else if(dataInspectPlan.getInspectPlanStartTime().getTime() > DateTimeUtils.asDate(LocalDate.now()).getTime()){
            dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.NotStarted.value());
            dataInspectPlanRepository.updateRecordTime(dataInspectPlan.getId());
        }
        dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
        //清除之前所选择的检查项目
        dataInspectPlanItemRepository.deleteDataInspectPlanItemByInspectPlanId(dataInspectPlan.getId());
        List<Integer> itemPlanIds = dataInspectPlanBo.getItemPlanIds();
        if(CollectionUtil.isNotEmpty(itemPlanIds)){
            itemPlanIds.forEach(getIntegerConsumer(dataInspectPlan));
        }
        //判断计划开始时间是否当前时间
        if(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), new Date()).
                equals(DateTimeUtils.format(DateTimeUtils.PATTERN.DATE.value(), dataInspectPlan.getInspectPlanStartTime())) && "1".equals(dataInspectPlan1.getActivity())){
            //异步生成 对应的巡检工单
            CompletableFuture noArgsFuture = CompletableFuture.runAsync(new Runnable() {
                @SneakyThrows
                @Override
                public void run() {
                    log.info("异步生成巡检工单线程开启");
                    runAsync(dataInspectPlan,projectCode,scopeOfUse);
                }
            });
        }
    }


    @Override
    public DataInspectPlanDetailVo findDataInspectPlanById(int id) {
        DataInspectPlanDetailVo dataInspectPlanDetailVo = new DataInspectPlanDetailVo();
        DataInspectPlan dataInspectPlan = dataInspectPlanRepository.searchDataInspectPlanByPrimaryKey(id);
        if(dataInspectPlan != null){
            String scopeOfUse = dataInspectPlan.getScopeOfUse();
            String[] split = scopeOfUse.split(";");
            dataInspectPlanDetailVo.setScopeOfUseList(Arrays.asList(split));
        }
        BeanUtils.copyProperties(dataInspectPlan,dataInspectPlanDetailVo);
        //查询对应巡检计划的检查项目
        List<InspectPlanItem> inspectPlanItems = dataInspectPlanItemRepository.searchDataInspectPlanItemByInspectPlanId(id);
        dataInspectPlanDetailVo.setInspectPlanItems(inspectPlanItems);
        return dataInspectPlanDetailVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void disableRecoveryDataInspectPlan(DisableRecoveryStatus disableRecoveryStatus) throws ArgumentErrorException {
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        //比较计划开始时间与当前时间
        if(disableRecoveryStatus.getActivity().equals("1")){
            //获取当前天的时间戳
            long nowDay = DateTimeUtils.asDate(LocalDate.now()).getTime();
            List<Integer> ids = disableRecoveryStatus.getIds();
            for (Integer id : ids) {
                DataInspectPlan dataInspectPlan = dataInspectPlanRepository.searchDataInspectPlanByPrimaryKey(id);

                long time = dataInspectPlan.getInspectPlanStartTime().getTime();
                String inspectPlanCycle = dataInspectPlan.getInspectPlanCycle();
                InSpectConsts.InspectPlanCycle inspectPlanCycle1 = InSpectConsts.InspectPlanCycle.byValue(inspectPlanCycle);
                Date recordTime = dataInspectPlan.getRecordTime();
                //比较计划开始时间与当前时间
                if(nowDay >= time){
                    if(nowDay == time || (dataInspectPlan.getRecordTime() != null && nowDay == recordTime.getTime())){
                        dataInspectPlan.setRecordTime(new Date());
                    }
                    dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.HaveInHand.value());
                }
                dataInspectPlan.setActivity(disableRecoveryStatus.getActivity());
                dataInspectPlan.setUpdator(headerUser);
                dataInspectPlan.setUpdateTime(new Date());
                dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
                String scopeOfUseStr = dataInspectPlan.getScopeOfUse();
                String[] split = scopeOfUseStr.split(";");
                //判断计划开始时间是否当前时间
                if((nowDay == time) || (recordTime != null && nowDay == recordTime.getTime())){
                    //异步生成 对应的巡检工单
                    CompletableFuture noArgsFuture = CompletableFuture.runAsync(new Runnable() {
                        @SneakyThrows
                        @Override
                        public void run() {
                            log.info("异步生成巡检工单线程开启");
                            runAsync(dataInspectPlan, String.valueOf(dataInspectPlan.getStationId()),Arrays.asList(split));
                        }
                    });
                }
            }
        }else {
            disableRecoveryStatus.setUpdator(headerUser);
            dataInspectPlanRepository.disableRecoveryDataInspectPlan(disableRecoveryStatus);
        }
    }

    @Override
    public PageResult findDataInspectPlan(DataInspectPlanQueryCondition dataInspectPlanQueryCondition) throws ArgumentErrorException {
        PageResult result = null;
        Long headerUser = myHeaderUtil.getHeaderUser();
        if(headerUser == null){
            throw new ArgumentErrorException("用户登录信息已过期，请重新登录！");
        }
        String projectCode = ServerContextHolder.getContext().getProjectCode();
        if(StringUtil.isEmpty(projectCode)){
            throw new ArgumentErrorException("请选择项目查询巡检计划!");
        }
        dataInspectPlanQueryCondition.setStationId(Long.valueOf(projectCode));
        if(dataInspectPlanQueryCondition.getOrgId() == null){
            //查询当前登录人的部门以及负责部门和子部门
            ResponseEntity<ResultObject<List<Organization>>> responseEntity = authorityClient.getSubOrgByLoginUser(Long.valueOf(projectCode),headerUser,true);
            if(responseEntity != null){
                if(responseEntity.getStatusCode().is2xxSuccessful()){
                    List<Organization> organizations = responseEntity.getBody().getResults();
                    if(CollectionUtil.isNotEmpty(organizations)){
                        List<Long> orgIds = organizations.stream().map(Organization::getId).collect(Collectors.toList());
                        dataInspectPlanQueryCondition.setOrgIds(orgIds);
                    }
                }
            }
        }else {
            dataInspectPlanQueryCondition.setOrgIds(Arrays.asList(dataInspectPlanQueryCondition.getOrgId()));
        }
        PageHelper.startPage(dataInspectPlanQueryCondition.getPage(), dataInspectPlanQueryCondition.getSize());
        List<DataInspectPlanVo> dataInspectPlanVos = dataInspectPlanRepository.findDataInspectPlan(dataInspectPlanQueryCondition);
        if(CollectionUtil.isNotEmpty(dataInspectPlanVos)){
            dataInspectPlanVos.forEach(dataInspectPlanVo -> {
                if(dataInspectPlanVo.getScopeOfUse().equals("00")){
                    dataInspectPlanVo.setScopeOfUseTitle("全部");
                }
            });
        }
        PageInfo<DataInspectPlanVo> pageInfo = new PageInfo<>(dataInspectPlanVos);
        result = new PageResult(dataInspectPlanQueryCondition.getPage(), dataInspectPlanQueryCondition.getSize(), (int)pageInfo.getTotal(), dataInspectPlanVos);
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void inspectPlanToOrder() throws InterruptedException {
        //查询巡检计划周期状态为未开始或进行中的的巡检计划
        List<DataInspectPlan> dataInspectPlans = dataInspectPlanRepository.findDataInspectPlanByInspectPlanCycleStatus();
        if(CollectionUtil.isNotEmpty(dataInspectPlans)){
            for (DataInspectPlan dataInspectPlan : dataInspectPlans) {
                Date inspectPlanEndTime = dataInspectPlan.getInspectPlanEndTime();
                Date inspectPlanStartTime = dataInspectPlan.getInspectPlanStartTime();
                Date recordTime = dataInspectPlan.getRecordTime();
                InSpectConsts.InspectPlanCycleStatus inspectPlanCycleStatus = InSpectConsts.InspectPlanCycleStatus.byValue(dataInspectPlan.getInspectPlanCycleStatus());
                InSpectConsts.InspectPlanCycle inspectPlanCycle = InSpectConsts.InspectPlanCycle.byValue(dataInspectPlan.getInspectPlanCycle());
                String scopeOfUse = dataInspectPlan.getScopeOfUse();
                String[] split = scopeOfUse.split(";");
                //获取当前天的时间戳
                long nowDay = DateTimeUtils.asDate(LocalDate.now()).getTime();
                //正常的计划执行生成工单
                if("1".equals(dataInspectPlan.getActivity())){
                    //判断当前巡检计划是否过期
                    if( inspectPlanEndTime != null && inspectPlanEndTime.getTime() < nowDay){
                        dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.HasEnded.value());
                        dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
                        continue;
                    }else {
                        switch (inspectPlanCycleStatus){
                            case NotStarted:
                                //先判断未开始的巡检计划的开始时间是与当前时间计较 如果是当前时间生成对应工单
                                if(inspectPlanStartTime.getTime() == nowDay){
                                    dataInspectPlan.setInspectPlanCycleStatus(InSpectConsts.InspectPlanCycleStatus.HaveInHand.value());
                                    dataInspectPlan.setRecordTime(new Date());
                                    dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
                                    runAsync(dataInspectPlan, String.valueOf(dataInspectPlan.getStationId()),Arrays.asList(split));
                                }
                                break;
                            case HaveInHand:
                                Calendar calendar = Calendar.getInstance();
                                if(recordTime == null){
                                    calendar.setTime(inspectPlanStartTime);
                                }else {
                                    calendar.setTime(recordTime);
                                }
                                switch (inspectPlanCycle){
                                    case Day:
                                        calendar.add(Calendar.DATE, 1);
                                        break;
                                    case Week:
                                        calendar.add(Calendar.DATE, 7);
                                        break;
                                    case Month:
                                        calendar.add(Calendar.MONTH, 1);
                                        break;
                                    case Season:
                                        calendar.add(Calendar.MONTH, 3);
                                        break;
                                    case Year:
                                        calendar.add(Calendar.YEAR, 1);
                                        break;
                                }
                                Date orderCreateDay = calendar.getTime();
                                //判断当前时间与下次生成工单的时间是否相同
                                if(orderCreateDay.getTime() == nowDay){
                                    dataInspectPlan.setRecordTime(new Date());
                                    dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
                                    runAsync(dataInspectPlan, String.valueOf(dataInspectPlan.getStationId()),Arrays.asList(split));
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }else {
                    //被禁用的计划记录周期生成工单时间
                    Calendar calendar = Calendar.getInstance();
                    if(recordTime == null){
                        calendar.setTime(inspectPlanStartTime);
                    }else {
                        calendar.setTime(recordTime);
                    }
                    switch (inspectPlanCycle){
                        case Day:
                            calendar.add(Calendar.DATE, 1);
                            break;
                        case Week:
                            calendar.add(Calendar.DATE, 7);
                            break;
                        case Month:
                            calendar.add(Calendar.MONTH, 1);
                            break;
                        case Season:
                            calendar.add(Calendar.MONTH, 3);
                            break;
                        case Year:
                            calendar.add(Calendar.YEAR, 1);
                            break;
                    }
                    Date orderCreateDay = calendar.getTime();
                    dataInspectPlan.setRecordTime(orderCreateDay);
                    //判断当前时间与下次生成工单的时间是否相同
                    if(orderCreateDay.getTime() == nowDay){
                        dataInspectPlan.setRecordTime(new Date());
                        dataInspectPlanRepository.updateDataInspectPlanByPrimaryKeySelective(dataInspectPlan);
                    }
                }

            }
        }
    }





}